/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyao89.view.zweb.views;

import com.zyao89.view.zweb.utils.ZLog;
import ohos.agp.components.webengine.*;
import ohos.app.Context;
import ohos.utils.net.Uri;

/**
 * 具体类实现
 *
 * @since 2021-06-07
 */
class BrowserAgentEx extends BrowserAgent {
    public BrowserAgentEx(Context context) {
        super(context);
    }

    private FixedOnReceivedTitle mFixedOnReceivedTitle;

    void setFixedOnReceivedTitle(FixedOnReceivedTitle fixedOnReceivedTitle) {
        mFixedOnReceivedTitle = fixedOnReceivedTitle;
        fixedOnReceivedTitle.setBrowserAgentEx(this);
    }

    @Override
    public void onProgressUpdated(WebView webView, int newValue) {
        // 加载进度变更时自定义处理
        super.onProgressUpdated(webView, newValue);
    }

    @Override
    public boolean onJsMessageShow(WebView webView, String url, String message, boolean isAlert, JsMessageResult result) {
        // alert警告框、confirm确认框
        return super.onJsMessageShow(webView, url, message, isAlert, result);
    }

    @Override
    public boolean onJsTextInput(WebView webView, String url, String message, String defaultInput, JsTextInputResult result) {
        // JsPrompt 提示框
        ZLog.with(this).z("onJsPrompt:" + url + "  message:" + message + "  d:" + defaultInput + "  ");
        return super.onJsTextInput(webView, url, message, defaultInput, result);
    }

    @Override
    public boolean onPickFiles(WebView webView, AsyncCallback<Uri[]> urisCallback, PickFilesParams params) {
        // 文件选择
        return super.onPickFiles(webView, urisCallback, params);
    }

    @Override
    public void onTitleUpdated(WebView webView, String value) {
        mFixedOnReceivedTitle.onReceivedTitle();
    }
}
